//任务队列以局部变量方式存在
#include "thread_pool2.cpp"

class thread_pool3 : public thread_pool
{
    private:
        threadSafe_queue<function_wrapper> pool_work_queue;
        typedef std::queue<function_wrapper> local_queue_type;
        static thread_local std::unique_ptr<local_queue_type> local_work_queue;
        void worker_thread()
        {
            local_work_queue.reset(new local_queue_type);
            while (!done)
                run_pending_task();
        }
    public:
        template<typename FunctionType>
        std::future<typename std::result_of<FunctionType()>::type>
            submit(FunctionType f)
        {
            typedef typename std::result_of<FunctionType()>::type result_type;
            std::packaged_task<result_type()> task(f);
            std::future<result_type> res(task.get_future());
            if(local_work_queue)
                local_work_queue->push(std::move(task));
            else
                pool_work_queue.push(std::move(task));
            return res;
        }
        void run_pending_task()
        {
            function_wrapper task;
            if(local_work_queue && !local_work_queue->empty())
            {
                task=std::move(local_work_queue->front());
                local_work_queue->pop();
                task();
            }
            else if(pool_work_queue.try_pop(task))
                task();
            else
                std::this_thread::yield();
        }
};